# -*- coding: utf-8 -*-
# created on 2017/01/19

from __future__ import division
from sympy import sympify
from mathsolver.functions.hanshu.helper import dairu_huajian
from mathsolver.functions.base import BaseFunction, BaseIneq
from mathsolver.functions.hanshu.dandiaoqujian import DanDiaoQuJian
from mathsolver.functions.hanshu.jiou_dandiao_budengshi import JiOuDanDiaoBuDengShi001
from mathsolver.functions.hanshu.jiou_panduan import OddEven


class DaiRuJieBuDengShi(BaseFunction):
    """解函数不等式：类型一，代入解不等式"""

    def solver(self, *args):
        func, ineq = args
        fx = sympify('%s(%s)' % (func.name, func.var))
        expr = func.expression

        left, op, right = ineq.value
        left = dairu_huajian([fx, expr], sympify(left))
        right = dairu_huajian([fx, expr], sympify(right))

        ineq = (left, op, right)
        self.steps.append(['', '根据函数表达式，将不等式化简为：%s %s %s' % ineq])
        self.output.append(BaseIneq(ineq))
        self.label.add("解函数不等式")
        return self


class JiOuDanDiaoJieBuDengShi(BaseFunction):
    """解函数不等式：类型二，函数奇偶性和单调性解不等式"""

    def solver(self, *args):
        func, ineq = args

        # 判断函数奇偶性
        jiou = OddEven().solver(func).output[0]

        # 计算单调区间
        up, down = DanDiaoQuJian().solver(func).output[0].value

        # 尝试 函数奇偶性与单调性解不等式
        if jiou != '非奇非偶函数':
            if up:
                return JiOuDanDiaoBuDengShi001().solver(
                    jiou, up, '增', ineq)
            elif down:
                return JiOuDanDiaoBuDengShi001().solver(
                    jiou, down, '减', ineq)
        else:
            raise ValueError("don't match, use dairu_jiebudengshi_001")


class HanShuBuDengShi(BaseFunction):
    """解函数不等式"""

    def solver(self, *args):
        try:
            return JiOuDanDiaoJieBuDengShi().solver(*args)
        except Exception:
            return DaiRuJieBuDengShi().solver(*args)


if __name__ == '__main__':
    pass
